package com.lam.common.base.page;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 表格分页数据对象
 * 
 * @author lam
 */
public class TableData implements Serializable {
	
	private static final long serialVersionUID = 1L;

	/** 列表数据 */
	protected List<?> records = Collections.emptyList();
	/** 总记录数 */
	private long total;
	
	/** 当前页码 */
	protected int pageNum;
	/** 每页条数 */
	protected int pageSize;

	/**
	 * 表格数据对象
	 */
	public TableData() {}

	/**
	 * 分页
	 * @param list 列表数据
	 * @param total 总记录数
	 */
	public TableData(List<?> list, long total) {
		this.records = list;
		this.total = total;
	}
	
	public long getTotal() {
		return total;
	}

	public TableData total(long total) {
		this.total = total;
		return this;
	}

	public List<?> getRecords(){
		return records == null ? Collections.emptyList():records;
	}

	public TableData records(List<?> records) {
		this.records = records;
		return this;
	}

	public int getPageNum() {
		return pageNum;
	}
	
	public int getCurrent() {
		return pageNum;
	}

	public int getSize() {
		return pageSize;
	}
	
	public long getPages() {
		if(this.total < 1 || pageSize < 1) {
			return 0;
		}
		if(this.total % pageSize == 0) {
			return this.total/pageSize;
		}
		
		return this.total/pageSize + 1;
	}
	
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setRecords(List<?> records) {
		this.records = records;
	}

	public void setTotal(long total) {
		this.total = total;
	}

}
